from selenium import webdriver
import time
from lxml import etree
import csv
import msvcrt
def get_driver(url):
	driver = webdriver.Chrome('/Program Files (x86)/Google/chromedriver.exe')
	driver.get(url)
	return driver


def get_page_source(driver, url):
	##函数睡眠1秒，等待网页响应和渲染
	time.sleep(1)
	page_source = driver.page_source
	return page_source

def get_xhtml(page_source):
	xhtml = etree.HTML(page_source)
	return xhtml

## 这是一个生成器函数
def parse_datas(xhtml, city):
    datas = {}
    items = xhtml.xpath('//div[@class="abstract-item clearfix"]')
    for item in items:
        datas['city'] = city
        # print(datas['city'])
        datas['name'] = item.xpath('.//a[@class="item-title"]/text()')[0].strip()
        # print(datas['name'])
        
        datas['grade'] = item.xpath('.//div[@class="item-eval-info clearfix"]//text()')[0].strip()
        # print(datas['grade'])
		# print(item.xpath('.//div[@class="poi-price"]/em/text()')[0].strip())
		# datas['price'] = item.xpath('.//div[@class="poi-price"]/em/text()')[0].strip()
		# data['type'] = item.xpath('.//div[@class="item-site-info clearfix" span] /text()')[0].strip()
        # data['area'] = item.xpath('.//div[@class="item-site-info clearfix" span] /text()')[0].strip()
		## 部分酒店没有消费人数的信息，如果没有，返回空字符串
        try:
            datas['AVGprice'] = item.xpath('.//div[@class="item-price-info"] //text()')[1].strip()
            # print(datas['AVGprice'])
            datas['grade'] = item.xpath('.//div[@class="poi-grade"]/text()')[0].strip()
        except IndexError:
            pass
        datas['type'] = item.xpath('.//div[@class="item-site-info clearfix"] //text()')[0].strip()
        datas['area'] = item.xpath('.//div[@class="item-site-info clearfix"] //text()')[2].strip()
        datas['address'] = item.xpath('.//div[@class="item-site-info clearfix"] //text()')[3].strip()
        # print(datas['type'],datas['area'],datas['address'])
        yield datas


def save_datas(datas):
	with open('./数据模拟工具/xiuxianyule.csv', 'a', encoding='GB18030', newline='') as c:
		fieldnames = ['city', 'name', 'AVGprice', 'grade', 'address','type','area']
		writer = csv.DictWriter(c, fieldnames=fieldnames)
		for data in datas:
			writer.writerow(data)

if __name__ == '__main__':
	city = '德清'
	url  = 'https://deqing.meituan.com/xiuxianyule'
	driver = get_driver(url)
	# xhtml = get_xhtml(page_source)
	for i in range(60):
		page_source = get_page_source(driver, url)
		xhtml = get_xhtml(page_source)
		datas = parse_datas(xhtml ,city)
		print(datas)
		save_datas(datas)
		if ord(msvcrt.getch())==100:
			continue
		else:
			driver.quit()
			break